var express = require('express');
var router = express.Router();
const userService = require('../service/userService');

//增加用户
router.post('/', async function (req, res) {
  let { username, name, phone, image, openId } = req.body;
  let data = await userService.insert({ username, name, phone, image, openId });
  res.send(data);
});

//查询所有用户
router.get('/', async function (req, res) {
  let { type, value, current, size } = req.query;
  let searchObj = {};
  //模糊查询
  if (type) {
    if (type == "phone" || "username"||"name") {
      searchObj[type] = { $regex: value };
    }
  }
  let data = await userService.find(searchObj,
    { current: current || 1, size: size || 10000 });
  res.send(data);
})

//根据ID查询学生
router.get('/:id', async function (req, res) {
  let id = req.params.id;
  let data = await userService.findById(id);
  res.send(data);
});

module.exports = router;
